package com.tools.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.login.R;
import com.tools.CommonThings;

import java.util.List;

public class GoodsAdapter extends ArrayAdapter<Goods> {
    private final int resourceId;
    private Context mContext;

    public GoodsAdapter(Context context, int resource, List<Goods> objects) {
        super(context, resource, objects);
        resourceId = resource;
        mContext = context;
    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(resourceId,null);
        }

        Goods goods = getItem(position);
        TextView goodsName = (TextView) convertView.findViewById(R.id.goods_name);
        TextView goodsRate = (TextView) convertView.findViewById(R.id.goods_rate);
        TextView goodsMinAmount = (TextView) convertView.findViewById(R.id.goods_min_amount);

        goodsName.setText(goods.getName());
        goodsRate.setText(CommonThings.invertPercent(goods.getAnnualInterestRate()));
        goodsMinAmount.setText(String.format(mContext.getResources().getString(R.string.minAmount), goods.getMinAmount()));

        return convertView;
    }
}
